﻿namespace GoogleMusicDesktop
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    internal class XmlHelper
    {
        private static XmlDocument xd = new XmlDocument();

        public static bool IsBytesXML(byte[] bytes)
        {
            try
            {
                if (((bytes[0] != 60) || (bytes[1] != 0x3f)) || (((bytes[2] != 120) || (bytes[3] != 0x6d)) || (bytes[4] != 0x6c)))
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void SaveToXmlfile(object obj, string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate);
            stream.SetLength(0L);
            TextWriter textWriter = new StreamWriter(stream, Encoding.Default);
            new XmlSerializer(obj.GetType()).Serialize(textWriter, obj);
            textWriter.Close();
        }

        public static byte[] XmlToByte(object obj)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                MemoryStream stream = new MemoryStream();
                serializer.Serialize((Stream) stream, obj);
                return stream.ToArray();
            }
            catch
            {
                return null;
            }
        }

        public static string XmlToString(object obj)
        {
            return Encoding.Default.GetString(XmlToByte(obj));
        }

        public static bool XmlTryParse(string str, Type objType, bool IsCheckTypeName, out object rtnObj)
        {
            return XmlTryParse(Encoding.Default.GetBytes(str), objType, IsCheckTypeName, out rtnObj);
        }

        public static bool XmlTryParse(byte[] bytes, Type objType, bool IsCheckTypeName, out object rtnObj)
        {
            try
            {
                if (((bytes[0] != 60) || (bytes[1] != 0x3f)) || (((bytes[2] != 120) || (bytes[3] != 0x6d)) || (bytes[4] != 0x6c)))
                {
                    rtnObj = null;
                    return false;
                }
                lock (xd)
                {
                    string xml = Encoding.Default.GetString(bytes);
                    xd.LoadXml(xml);
                    if (IsCheckTypeName && (objType.Name.ToString().ToUpper() != xd.DocumentElement.Name.ToUpper()))
                    {
                        rtnObj = null;
                        return false;
                    }
                    XmlSerializer serializer = new XmlSerializer(objType);
                    MemoryStream stream = new MemoryStream(bytes);
                    object obj2 = serializer.Deserialize(stream);
                    rtnObj = obj2;
                }
                if (rtnObj == null)
                {
                    return false;
                }
                return true;
            }
            catch
            {
                rtnObj = null;
                return false;
            }
        }
    }
}

